<template>
  <div class="art-management">
    <!-- 操作栏 -->
    <div class="operation-bar">
      <el-input
        v-model="searchText"
        placeholder="搜索作品ID/名称/作者"
        style="width: 360px"
        clearable
      >
        <template #prefix>
          <el-icon><Search /></el-icon>
        </template>
      </el-input>
      <el-button type="primary" @click="handleAdd">
        <el-icon><Plus /></el-icon>新增作品
      </el-button>
    </div>

    <!-- 数据表格 -->
    <el-table 
      :data="filteredArts" 
      border 
      style="width: 100%"
      header-row-class-name="table-header"
      stripe
      highlight-current-row
    >
      <el-table-column prop="artId" label="作品ID" width="130" sortable align="center" />
      <el-table-column prop="title" label="作品名称" width="180" />
      <el-table-column prop="artist" label="作者" width="120" />
      <el-table-column prop="style" label="艺术风格" width="120">
        <template #default="{ row }">
          <el-tag effect="plain" :type="styleTagType[row.style]">
            {{ row.style }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="price" label="估价" width="120" align="center">
        <template #default="{ row }">
          <span style="color: #67C23A;">¥{{ row.price.toFixed(2) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="作品状态" width="140" align="center">
        <template #default="{ row }">
          <el-tag :type="statusTagType[row.status]" effect="light">
            {{ statusMap[row.status] }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createDate" label="创作日期" width="140" sortable />
      <el-table-column prop="description" label="作品描述" width="220">
        <template #default="{ row }">
          <div class="content-preview">{{ row.description }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="160" fixed="right" align="center">
        <template #default="{ row }">
          <el-button size="small" type="primary" plain @click="handleEdit(row)">编辑</el-button>
          <el-button
            size="small"
            type="danger"
            plain
            @click="handleDelete(row.artId)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加/编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="isEditMode ? '编辑作品信息' : '新增作品'"
    >
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="100px"
        label-position="left"
      >
        <el-form-item label="作品ID" prop="artId">
          <el-input 
            v-model="formData.artId" 
            :disabled="isEditMode"
            placeholder="请输入作品ID（如：ART2024001）"
          />
        </el-form-item>
        <el-form-item label="作品名称" prop="title">
          <el-input 
            v-model="formData.title"
            placeholder="请输入作品名称"
          />
        </el-form-item>
        <el-form-item label="作者" prop="artist">
          <el-input 
            v-model="formData.artist"
            placeholder="请输入作者"
          />
        </el-form-item>
        <el-form-item label="艺术风格" prop="style">
          <el-select 
            v-model="formData.style"
            placeholder="请选择艺术风格"
            style="width: 100%"
          >
            <el-option
              v-for="item in styles"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="估价" prop="price">
          <el-input-number
            v-model="formData.price"
            :min="0"
            :step="5000"
            controls-position="right"
            style="width: 100%"
            :precision="2"
            placeholder="请输入估价"
          >
            <template #prefix>¥</template>
          </el-input-number>
        </el-form-item>
        <el-form-item label="作品状态" prop="status">
          <el-select 
            v-model="formData.status"
            placeholder="请选择状态"
            style="width: 100%"
          >
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="创作日期" prop="createDate">
          <el-date-picker
            v-model="formData.createDate"
            type="date"
            placeholder="选择创作日期"
            value-format="YYYY-MM-DD"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="作品描述" prop="description">
          <el-input 
            type="textarea"
            v-model="formData.description" 
            rows="4"
            placeholder="请输入作品描述"
            show-word-limit
            maxlength="200"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确认提交</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, computed, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 艺术风格配置
const styles = [
  '古典主义',
  '印象派',
  '抽象派',
  '写实主义',
  '表现主义',
  '超现实主义'
]

// 状态配置
const statusOptions = [
  { value: 0, label: '在售' },
  { value: 1, label: '已售' },
  { value: 2, label: '预定中' }
]

// 标签样式映射
const styleTagType = {
  '古典主义': 'info',
  '印象派': 'warning',
  '抽象派': 'danger',
  '写实主义': 'success',
  '表现主义': 'primary',
  '超现实主义': ''
}

const statusTagType = {
  0: 'success',
  1: 'warning',
  2: 'danger'
}

const statusMap = {
  0: '在售',
  1: '已售',
  2: '预定中'
}

// 油画数据（7条）
const artsList = ref([
  {
    artId: 'ART2024001',
    title: '星空下的咖啡馆',
    artist: '文森特·梵高',
    style: '印象派',
    price: 4500000.00,
    status: 0,
    createDate: '1888-09-01',
    description: '典型的后印象派风格，强烈的色彩对比与旋转笔触'
  },
  {
    artId: 'ART2024002',
    title: '蒙娜丽莎',
    artist: '达芬奇',
    style: '古典主义',
    price: 850000000.00,
    status: 1,
    createDate: '1503-01-01',
    description: '文艺复兴时期肖像画巅峰之作'
  },
  {
    artId: 'ART2024003',
    title: '格尔尼卡',
    artist: '毕加索',
    style: '抽象派',
    price: 200000000.00,
    status: 0,
    createDate: '1937-05-01',
    description: '立体主义风格的反战巨作'
  },
  {
    artId: 'ART2024004',
    title: '日出·印象',
    artist: '克劳德·莫奈',
    style: '印象派',
    price: 35000000.00,
    status: 2,
    createDate: '1872-11-13',
    description: '印象派命名来源的代表作'
  },
  {
    artId: 'ART2024005',
    title: '戴珍珠耳环的少女',
    artist: '维米尔',
    style: '写实主义',
    price: 180000000.00,
    status: 0,
    createDate: '1665-01-01',
    description: '北方巴洛克艺术典范之作'
  },
  {
    artId: 'ART2024006',
    title: '记忆的永恒',
    artist: '达利',
    style: '超现实主义',
    price: 150000000.00,
    status: 0,
    createDate: '1931-01-01',
    description: '超现实主义标志性软钟表意象'
  },
  {
    artId: 'ART2024007',
    title: '呐喊',
    artist: '爱德华·蒙克',
    style: '表现主义',
    price: 120000000.00,
    status: 1,
    createDate: '1893-01-01',
    description: '表现主义最具辨识度的作品'
  }
])

// 搜索功能
const searchText = ref('')
const filteredArts = computed(() => {
  const search = searchText.value.toLowerCase()
  return artsList.value.filter(art => 
    art.artId.toLowerCase().includes(search) ||
    art.title.toLowerCase().includes(search) ||
    art.artist.toLowerCase().includes(search) ||
    art.style.toLowerCase().includes(search)
  )
})

// 对话框相关状态
const dialogVisible = ref(false)
const isEditMode = ref(false)
const formRef = ref(null)
const formData = reactive({
  artId: '',
  title: '',
  artist: '',
  style: '印象派',
  price: 0,
  status: 0,
  createDate: new Date().toISOString().split('T')[0],
  description: ''
})

// 表单验证规则
const formRules = {
  artId: [
    { required: true, message: '作品ID不能为空', trigger: 'blur' },
    { pattern: /^ART\d{7}$/, message: '格式：ART+7位数字', trigger: 'blur' }
  ],
  title: [
    { required: true, message: '请输入作品名称', trigger: 'blur' },
    { min: 2, max: 50, message: '长度在2到50个字符', trigger: 'blur' }
  ],
  price: [
    { required: true, message: '请输入估价', trigger: 'blur' },
    { type: 'number', min: 0, max: 1000000000, message: '估价范围0-10亿元', trigger: 'blur' }
  ],
  style: { required: true, message: '请选择艺术风格', trigger: 'change' }
}

// 操作函数
const handleAdd = () => {
  isEditMode.value = false
  resetForm()
  dialogVisible.value = true
}

const handleEdit = (row) => {
  isEditMode.value = true
  Object.assign(formData, { ...row })
  dialogVisible.value = true
}

const handleDelete = (artId) => {
  ElMessageBox.confirm('确定要删除该作品信息？', '警告', {
    confirmButtonText: '确认删除',
    cancelButtonText: '取消',
    type: 'warning',
    confirmButtonClass: 'el-button--danger'
  }).then(() => {
    artsList.value = artsList.value.filter(p => p.artId !== artId)
    ElMessage.success('信息删除成功')
  }).catch(() => {})
}

const submitForm = async () => {
  await formRef.value.validate()
  if (isEditMode.value) {
    const index = artsList.value.findIndex(p => p.artId === formData.artId)
    artsList.value.splice(index, 1, { ...formData })
  } else {
    if (artsList.value.some(p => p.artId === formData.artId)) {
      return ElMessage.warning('作品ID已存在')
    }
    artsList.value.push({ ...formData })
  }
  dialogVisible.value = false
  ElMessage.success(isEditMode.value ? '更新成功' : '新增成功')
}

const resetForm = () => {
  Object.assign(formData, {
    artId: '',
    title: '',
    artist: '',
    style: '印象派',
    price: 0,
    status: 0,
    createDate: new Date().toISOString().split('T')[0],
    description: ''
  })
}
</script>

<style scoped lang="scss">
.art-management {
  padding: 24px;
  background: #ffffff;
  min-height: 100vh;

  .operation-bar {
    padding: 16px 24px;
    background: #ffffff;
    border-radius: 8px;
    box-shadow: 0 2px 12px rgba(0,0,0,0.05);
    margin-bottom: 24px;
    display: flex;
    align-items: center;
    gap: 16px;
    border: 1px solid #f0f0f0;
  }

  :deep(.table-header) th {
    background-color: #f8f9fa !important;
    color: #606266;
    font-weight: 600;
  }

  .el-table {
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 2px 12px rgba(0,0,0,0.05);
    background: #ffffff;
    border: 1px solid #f0f0f0;
    
    .el-tag {
      font-size: 13px;
      padding: 0 8px;
      height: 26px;
      line-height: 24px;
    }
  }

  .content-preview {
    display: -webkit-box;
    .content-preview {
      display: -webkit-box;
      -webkit-line-clamp: 2;
      line-clamp: 2; // 添加标准属性
      -webkit-box-orient: vertical;
      overflow: hidden;
    }
    -webkit-box-orient: vertical;
    overflow: hidden;
    line-height: 1.5;
    color: #606266;
  }
}

.el-dialog {
  border-radius: 12px;
  background: #ffffff;
  
  .el-dialog__header {
    border-bottom: 1px solid #f0f0f0;
    margin: 0 24px;
    padding: 24px 0 16px;
    color: #303133;
  }
  
  .el-dialog__body {
    padding: 24px 32px;
    color: #606266;
  }
  
  .el-dialog__footer {
    border-top: 1px solid #f0f0f0;
    padding: 16px 24px;
  }
}

.el-form-item {
  margin-bottom: 20px;
  :deep(.el-form-item__label) {
    color: #606266;
  }
}
</style>